//
//  LJFriendsGroup.m
//  LJClientKit
//
//  Created by Anton Chebotaev on 2/10/09.
//  Copyright 2009 AntonChebotaev. All rights reserved.
//

#import "LJFriendsGroup.h"

// Coding constants
NSString * const LJFriendsGroupCodingID = @"LJFriendsGroupCodingID";
NSString * const LJFriendsGroupCodingName = @"LJFriendsGroupCodingName";
NSString * const LJFriendsGroupCodingSortOrder = @"LJFriendsGroupCodingSortOrder";
NSString * const LJFriendsGroupCodingPublicGroup = @"LJFriendsGroupCodingPublicGroup";

// Exceptions
NSString * const LJFriendsGroupBadInitException = @"LJFriendsGroupBadInitException";


@implementation LJFriendsGroup

@synthesize groupID;
@synthesize name;
@synthesize sortOrder;
@synthesize publicGroup;

- (id)init {
	[self dealloc];
    @throw [NSException exceptionWithName:LJFriendsGroupBadInitException
								   reason:@"Initialize LJFriendsGroup with simple init."
								 userInfo:nil];
    return nil;	
}

- (id)initWithID:(int)aGroupID name:(NSString *)aName sortOrder:(int)aSortOrder publicMode:(BOOL)newIsPublic {
	self = [super init];
	if (self) {
		groupID = aGroupID;
		name = [aName retain];
		sortOrder = aSortOrder;
		publicGroup = newIsPublic;
	}
	return self;
}
+ (id)groupWithID:(int)aGroupID name:(NSString *)aName sortOrder:(int)aSortOrder publicMode:(BOOL)isPublic {
	LJFriendsGroup *group = [[LJFriendsGroup alloc] initWithID:aGroupID name:aName sortOrder:aSortOrder publicMode:isPublic];
	return [group autorelease];	
}

- (void)dealloc {
	[name release];
	[super dealloc];
}

- (id)initWithCoder:(NSCoder *)decoder {
	self = [super init];
    if (self) {
		
		groupID = [[decoder decodeObjectForKey:LJFriendsGroupCodingID] intValue];
		sortOrder = [[decoder decodeObjectForKey:LJFriendsGroupCodingSortOrder] intValue];
        name = [[decoder decodeObjectForKey:LJFriendsGroupCodingName] retain];
		publicGroup = [[decoder decodeObjectForKey:LJFriendsGroupCodingSortOrder] boolValue];
		
        NSAssert(name != nil, @"LJFriendsGroup decoded without a name.");
    }
    return self;	
}
- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:[NSNumber numberWithInt:groupID] forKey:LJFriendsGroupCodingID];
    [encoder encodeObject:name forKey:LJFriendsGroupCodingName];
    [encoder encodeObject:[NSNumber numberWithInt:sortOrder] forKey:LJFriendsGroupCodingSortOrder];
    [encoder encodeObject:[NSNumber numberWithBool:publicGroup] forKey:LJFriendsGroupCodingPublicGroup];
}

- (NSString *)description {
	return [NSString stringWithFormat:@"%@/%d", name,  groupID];
}

- (BOOL)isEqual:(id)object {
	return [[self description] isEqual:[object description]];
}

- (NSUInteger)hash {
	return [[self description] hash];
}

@end
